Package org.javacommerce.paypal

Source Code of org.javacommerce.paypal.APIUtil

/**
*
*/
package org.javacommerce.paypal;

import java.io.IOException;
import java.io.StringWriter;
import java.math.BigInteger;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.ValidationException;

import CoreComponentTypes.apis.ebay.BasicAmountType;
import eBLBaseComponents.apis.ebay.AddressType;
import eBLBaseComponents.apis.ebay.CountryCodeType;
import eBLBaseComponents.apis.ebay.CreditCardDetailsType;
import eBLBaseComponents.apis.ebay.CreditCardTypeType;
import eBLBaseComponents.apis.ebay.CurrencyCodeType;
import eBLBaseComponents.apis.ebay.DoDirectPaymentRequestDetailsType;
import eBLBaseComponents.apis.ebay.DoExpressCheckoutPaymentRequestDetailsType;
import eBLBaseComponents.apis.ebay.PayerInfoType;
import eBLBaseComponents.apis.ebay.PaymentActionCodeType;
import eBLBaseComponents.apis.ebay.PaymentDetailsItemType;
import eBLBaseComponents.apis.ebay.PaymentDetailsType;
import eBLBaseComponents.apis.ebay.PersonNameType;
import eBLBaseComponents.apis.ebay.SetExpressCheckoutRequestDetailsType;

/**
* @author Michael Blanton (mike@mikeblanton.com)
*/
public class APIUtil {
 
  private static final String PARAM_MERCHANT_SESSION_ID = "merchantSessionId";
  private static final String PARAM_IP_ADDRESS = "ipAddress";
  private static final String PARAM_EXP_YEAR = "expYear";
  private static final String PARAM_EXP_MONTH = "expMonth";
  private static final String PARAM_CVV2 = "cvv2";
  private static final String PARAM_CREDIT_CARD_TYPE = "creditCardType";
  private static final String PARAM_CREDIT_CARD_NUMBER = "creditCardNumber";
  private static final String PARAM_LAST_NAME = "lastName";
  private static final String PARAM_FIRST_NAME = "firstName";
  private static final String PARAM_PAYER = "payer";
  private static final String PARAM_TAX_TOTAL_CURRENCY_ID = "taxTotal.currencyID";
  private static final String PARAM_TAX_TOTAL = "taxTotal";
  private static final String PARAM_SHIPPING_TOTAL_CURRENCY_ID = "shippingTotal.currencyID";
  private static final String PARAM_SHIPPING_TOTAL = "shippingTotal";
  private static final String DOT_TAX_CURRENCY_ID = ".tax.currencyID";
  private static final String DOT_AMOUNT_CURRENCY_ID = ".amount.currencyID";
  private static final String DOT_TAX = ".tax";
  private static final String DOT_QUANTITY = ".quantity";
  private static final String DOT_NUMBER = ".number";
  private static final String DOT_NAME = ".name";
  private static final String DOT_AMOUNT = ".amount";
  private static final String PARAM_ITEM = "item_";
  private static final String PARAM_NOTIFY_URL = "notifyURL";
  private static final String PARAM_ITEM_TOTAL_CURRENCY_ID = "itemTotal.currencyID";
  private static final String PARAM_ITEM_TOTAL = "itemTotal";
  private static final String PARAM_HANDLING_TOTAL_CURRENCY_ID = "handlingTotal.currencyID";
  private static final String PARAM_HANDLING_TOTAL = "handlingTotal";
  private static final Log LOG = LogFactory.getLog(APIUtil.class);
  private static Mapping MAPPING;

  private static final String PARAM_ADDRESS_CITY_NAME = "address.cityName";
  private static final String PARAM_ADDRESS_COUNTRY = "address.country";
  private static final String PARAM_ADDRESS_NAME = "address.name";
  private static final String PARAM_ADDRESS_PHONE = "address.phone";
  private static final String PARAM_ADDRESS_POSTAL_CODE = "address.postalCode";
  private static final String PARAM_ADDRESS_STATE_OR_PROVINCE = "address.stateOrProvince";
  private static final String PARAM_ADDRESS_STREET1 = "address.street1";
  private static final String PARAM_ADDRESS_STREET2 = "address.street2";
  private static final String PARAM_ADDRESS_OVERRIDE = "addressOverride";
  private static final String PARAM_BUYER_EMAIL = "buyerEmail";
  private static final String PARAM_CANCEL_URL = "cancelURL";
  private static final String PARAM_CPP_HEADER_BACK_COLOR = "cppHeaderBackColor";
  private static final String PARAM_CPP_HEADER_BORDER_COLOR = "cppHeaderBorderColor";
  private static final String PARAM_CPP_HEADER_IMAGE = "cppHeaderImage";
  private static final String PARAM_CPP_PAYFLOW_COLOR = "cppPayflowColor";
  private static final String PARAM_CUSTOM = "custom";
  private static final String PARAM_INVOICE_ID = "invoiceID";
  private static final String PARAM_LOCALE_CODE = "localeCode";
  private static final String PARAM_MAX_AMOUNT = "maxAmount";
  private static final String PARAM_MAX_AMOUNT_CURRENCY_ID = "maxAmount.currencyID";
  private static final String PARAM_NO_SHIPPING = "noShipping";
  private static final String PARAM_ORDER_DESCRIPTION = "orderDescription";
  private static final String PARAM_ORDER_TOTAL = "orderTotal";
  private static final String PARAM_ORDER_TOTAL_CURRENCY_ID = "orderTotal.currencyID";
  private static final String PARAM_PAGE_STYLE = "pageStyle";
  private static final String PARAM_PAYMENT_ACTION = "paymentAction";
  private static final String PARAM_REQ_CONFIRM_SHIPPING = "reqConfirmShipping";
  private static final String PARAM_RETURN_URL = "returnURL";
  public static final String PARAM_TOKEN = "token";
 
  static {
    MAPPING = new Mapping();
    try {
      MAPPING.loadMapping(APIUtil.class.getClassLoader().getResource("castor/mapping.xml"));
    } catch (IOException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("Error loading castor-mapping file: " + e.getLocalizedMessage(), e);
      }
    } catch (MappingException e) {
      if (LOG.isErrorEnabled()) {
        LOG.error("Error loading castor-mapping file: " + e.getLocalizedMessage(), e);
      }
    }
  }

  public static final SetExpressCheckoutRequestDetailsType buildSetExpressCheckoutDetailsRequest(
      HttpServletRequest _request) {
    SetExpressCheckoutRequestDetailsType request = new SetExpressCheckoutRequestDetailsType();

    if (_request.getParameter(PARAM_ADDRESS_OVERRIDE) != null) {
      if (_request.getParameter(PARAM_ADDRESS_OVERRIDE).equals("1")) {
        // Address
        request.setAddress(buildAddress(_request));
      }
      request.setAddressOverride(_request
          .getParameter(PARAM_ADDRESS_OVERRIDE));
    }
    if (_request.getParameter(PARAM_BUYER_EMAIL) != null) {
      request.setBuyerEmail(_request.getParameter(PARAM_BUYER_EMAIL));
    }
    if (_request.getParameter(PARAM_CANCEL_URL) != null) {
      request.setCancelURL(_request.getParameter(PARAM_CANCEL_URL));
    }
    if (_request.getParameter(PARAM_CPP_HEADER_BACK_COLOR) != null) {
      request.setCppHeaderBackColor(_request
          .getParameter(PARAM_CPP_HEADER_BACK_COLOR));
    }
    if (_request.getParameter(PARAM_CPP_HEADER_BORDER_COLOR) != null) {
      request.setCppHeaderBorderColor(_request
          .getParameter(PARAM_CPP_HEADER_BORDER_COLOR));
    }
    if (_request.getParameter(PARAM_CPP_HEADER_IMAGE) != null) {
      request.setCppHeaderImage(_request
          .getParameter(PARAM_CPP_HEADER_IMAGE));
    }
    if (_request.getParameter(PARAM_CPP_PAYFLOW_COLOR) != null) {
      request.setCppPayflowColor(_request
          .getParameter(PARAM_CPP_PAYFLOW_COLOR));
    }
    if (_request.getParameter(PARAM_CUSTOM) != null) {
      request.setCustom(_request.getParameter(PARAM_CUSTOM));
    }
    if (_request.getParameter(PARAM_INVOICE_ID) != null) {
      request.setInvoiceID(_request.getParameter(PARAM_INVOICE_ID));
    }
    if (_request.getParameter(PARAM_LOCALE_CODE) != null) {
      request.setLocaleCode(_request.getParameter(PARAM_LOCALE_CODE));
    }
    if (_request.getParameter(PARAM_MAX_AMOUNT) != null) {
      BasicAmountType maxAmount = new BasicAmountType();
      maxAmount.set_value(_request.getParameter(PARAM_MAX_AMOUNT));
      if (_request.getParameter(PARAM_MAX_AMOUNT_CURRENCY_ID) != null) {
        maxAmount.setCurrencyID(CurrencyCodeType.fromString(_request
            .getParameter(PARAM_MAX_AMOUNT_CURRENCY_ID)));
      }
      request.setMaxAmount(maxAmount);
    }
    if (_request.getParameter(PARAM_NO_SHIPPING) != null) {
      request.setNoShipping(_request.getParameter(PARAM_NO_SHIPPING));
    }
    if (_request.getParameter(PARAM_ORDER_DESCRIPTION) != null) {
      request.setOrderDescription(_request
          .getParameter(PARAM_ORDER_DESCRIPTION));
    }
    if (_request.getParameter(PARAM_ORDER_TOTAL) != null) {
      BasicAmountType orderTotal = new BasicAmountType();
      orderTotal.set_value(_request.getParameter(PARAM_ORDER_TOTAL));
      if (_request.getParameter(PARAM_ORDER_TOTAL_CURRENCY_ID) != null) {
        orderTotal.setCurrencyID(CurrencyCodeType.fromString(_request
            .getParameter(PARAM_ORDER_TOTAL_CURRENCY_ID)));
      }
      request.setOrderTotal(orderTotal);
    }
    if (_request.getParameter(PARAM_PAGE_STYLE) != null) {
      request.setPageStyle(_request.getParameter(PARAM_PAGE_STYLE));
    }
    if (_request.getParameter(PARAM_PAYMENT_ACTION) != null) {
      request.setPaymentAction(PaymentActionCodeType.fromString(_request
          .getParameter(PARAM_PAYMENT_ACTION)));
    }
    if (_request.getParameter(PARAM_REQ_CONFIRM_SHIPPING) != null) {
      request.setReqConfirmShipping(_request
          .getParameter(PARAM_REQ_CONFIRM_SHIPPING));
    }
    if (_request.getParameter(PARAM_RETURN_URL) != null) {
      request.setReturnURL(_request.getParameter(PARAM_RETURN_URL));
    }
    if (_request.getParameter(PARAM_TOKEN) != null) {
      request.setToken(_request.getParameter(PARAM_TOKEN));
    }
    return request;
  }

  /**
   * @param _request
   * @return
   */
  private static final AddressType buildAddress(HttpServletRequest _request) {
    AddressType address = new AddressType();
    if (_request.getParameter(PARAM_ADDRESS_CITY_NAME) != null) {
      address.setCityName(_request
          .getParameter(PARAM_ADDRESS_CITY_NAME));
    }
    if (_request.getParameter(PARAM_ADDRESS_COUNTRY) != null) {
      address.setCountry(CountryCodeType.fromString(_request
          .getParameter(PARAM_ADDRESS_COUNTRY)));
    }
    if (_request.getParameter(PARAM_ADDRESS_NAME) != null) {
      address.setName(_request.getParameter(PARAM_ADDRESS_NAME));
    }
    if (_request.getParameter(PARAM_ADDRESS_PHONE) != null) {
      address
          .setPhone(_request
              .getParameter(PARAM_ADDRESS_PHONE));
    }
    if (_request.getParameter(PARAM_ADDRESS_POSTAL_CODE) != null) {
      address.setPostalCode(_request
          .getParameter(PARAM_ADDRESS_POSTAL_CODE));
    }
    if (_request.getParameter(PARAM_ADDRESS_STATE_OR_PROVINCE) != null) {
      address.setStateOrProvince(_request
          .getParameter(PARAM_ADDRESS_STATE_OR_PROVINCE));
    }
    if (_request.getParameter(PARAM_ADDRESS_STREET1) != null) {
      address.setStreet1(_request
          .getParameter(PARAM_ADDRESS_STREET1));
    }
    if (_request.getParameter(PARAM_ADDRESS_STREET2) != null) {
      address.setStreet2(_request
          .getParameter(PARAM_ADDRESS_STREET2));
    }
    return address;
  }

  public static final DoExpressCheckoutPaymentRequestDetailsType buildDoExpressCheckoutPaymentDetailsRequest(
      HttpServletRequest _request) {
    DoExpressCheckoutPaymentRequestDetailsType request = new DoExpressCheckoutPaymentRequestDetailsType();
    PaymentDetailsType details = buildPaymentDetails(_request);
    request.setPaymentDetails(details);
   
    if (_request.getParameter("payerID") != null) {
      request.setPayerID(_request.getParameter("payerID"));
    }
    if (_request.getParameter(PARAM_PAYMENT_ACTION) != null) {
      request.setPaymentAction(PaymentActionCodeType.fromString(_request
          .getParameter(PARAM_PAYMENT_ACTION)));
    }
    if (_request.getParameter(PARAM_TOKEN) != null) {
      request.setToken(_request.getParameter(PARAM_TOKEN));
    }
    return request;
  }

  /**
   * @param _request
   * @return
   */
  private static final PaymentDetailsType buildPaymentDetails(HttpServletRequest _request) {
    PaymentDetailsType details = new PaymentDetailsType();
    if (_request.getParameter(PARAM_CUSTOM) != null) {
      details.setCustom(_request.getParameter(PARAM_CUSTOM));
    }
    if (_request.getParameter(PARAM_HANDLING_TOTAL) != null) {
      BasicAmountType handlingTotal = new BasicAmountType();
      handlingTotal.set_value(_request.getParameter(PARAM_HANDLING_TOTAL));
      if (_request.getParameter(PARAM_HANDLING_TOTAL_CURRENCY_ID) != null) {
        handlingTotal.setCurrencyID(CurrencyCodeType.fromString(_request.getParameter(PARAM_HANDLING_TOTAL_CURRENCY_ID)));
      }
      else {
        handlingTotal.setCurrencyID(CurrencyCodeType.USD);
      }
      details.setHandlingTotal(handlingTotal);
    }
    if (_request.getParameter(PARAM_INVOICE_ID) != null) {
      details.setInvoiceID(_request.getParameter(PARAM_INVOICE_ID));
    }
    if (_request.getParameter(PARAM_ITEM_TOTAL) != null) {
      BasicAmountType itemTotal = new BasicAmountType();
      itemTotal.set_value(_request.getParameter(PARAM_ITEM_TOTAL));
      if (_request.getParameter(PARAM_ITEM_TOTAL_CURRENCY_ID) != null) {
        itemTotal.setCurrencyID(CurrencyCodeType.fromString(_request.getParameter(PARAM_ITEM_TOTAL_CURRENCY_ID)));
      }
      else {
        itemTotal.setCurrencyID(CurrencyCodeType.USD);
      }
      details.setItemTotal(itemTotal);
    }
    if (_request.getParameter(PARAM_NOTIFY_URL) != null) {
      details.setNotifyURL(_request.getParameter(PARAM_NOTIFY_URL));
    }
    if (_request.getParameter(PARAM_ORDER_DESCRIPTION) != null) {
      details.setOrderDescription(_request
          .getParameter(PARAM_ORDER_DESCRIPTION));
    }
    if (_request.getParameter(PARAM_ORDER_TOTAL) != null) {
      BasicAmountType orderTotal = new BasicAmountType();
      orderTotal.set_value(_request.getParameter(PARAM_ORDER_TOTAL));
      if (_request.getParameter(PARAM_ORDER_TOTAL_CURRENCY_ID) != null) {
        orderTotal.setCurrencyID(CurrencyCodeType.fromString(_request
            .getParameter(PARAM_ORDER_TOTAL_CURRENCY_ID)));
      }
      details.setOrderTotal(orderTotal);
    }
    // Loop through line items
    int lineNum = 0;
    if (_request.getParameter(PARAM_ITEM + lineNum + DOT_AMOUNT) != null &&
        _request.getParameter(PARAM_ITEM + lineNum + DOT_NAME) != null &&
        _request.getParameter(PARAM_ITEM + lineNum + DOT_NUMBER) != null &&
        _request.getParameter(PARAM_ITEM + lineNum + DOT_QUANTITY) != null &&
        _request.getParameter(PARAM_ITEM + lineNum + DOT_TAX) != null) {
      ArrayList<PaymentDetailsItemType> items = new ArrayList<PaymentDetailsItemType>();
      while (_request.getParameter(PARAM_ITEM + lineNum + DOT_AMOUNT) != null &&
          _request.getParameter(PARAM_ITEM + lineNum + DOT_NAME) != null &&
          _request.getParameter(PARAM_ITEM + lineNum + DOT_NUMBER) != null &&
          _request.getParameter(PARAM_ITEM + lineNum + DOT_QUANTITY) != null &&
          _request.getParameter(PARAM_ITEM + lineNum + DOT_TAX) != null) {
        PaymentDetailsItemType item = new PaymentDetailsItemType();
        BasicAmountType amount = new BasicAmountType();
        amount.set_value(_request.getParameter(PARAM_ITEM + lineNum + DOT_AMOUNT));
        if (_request.getParameter(PARAM_ITEM + lineNum + DOT_AMOUNT_CURRENCY_ID) != null) {
          amount.setCurrencyID(CurrencyCodeType.fromString(_request.getParameter(PARAM_ITEM + lineNum + DOT_AMOUNT_CURRENCY_ID)));
        }
        else {
          amount.setCurrencyID(CurrencyCodeType.USD);
        }
        item.setAmount(amount);
        item.setName(_request.getParameter(PARAM_ITEM + lineNum + DOT_NAME));
        item.setNumber(_request.getParameter(PARAM_ITEM + lineNum + DOT_NUMBER));
        item.setQuantity(new BigInteger(_request.getParameter(PARAM_ITEM + lineNum + DOT_QUANTITY)));
        BasicAmountType tax = new BasicAmountType();
        tax.set_value(_request.getParameter(PARAM_ITEM + lineNum + DOT_TAX));
        if (_request.getParameter(PARAM_ITEM + lineNum + DOT_TAX_CURRENCY_ID) != null) {
          tax.setCurrencyID(CurrencyCodeType.fromString(_request.getParameter(PARAM_ITEM + lineNum + DOT_TAX_CURRENCY_ID)));
        }
        else {
          tax.setCurrencyID(CurrencyCodeType.USD);
        }
        item.setTax(tax);
        items.add(item);
        lineNum++;
      }
      details.setPaymentDetailsItem((PaymentDetailsItemType[]) items.toArray(new PaymentDetailsItemType[items.size()]));
    }
    if (_request.getParameter(PARAM_SHIPPING_TOTAL) != null) {
      BasicAmountType shippingTotal = new BasicAmountType();
      shippingTotal.set_value(_request.getParameter(PARAM_SHIPPING_TOTAL));
      if (_request.getParameter(PARAM_SHIPPING_TOTAL_CURRENCY_ID) != null) {
        shippingTotal.setCurrencyID(CurrencyCodeType.fromString(_request
            .getParameter(PARAM_SHIPPING_TOTAL_CURRENCY_ID)));
      }
      else {
        shippingTotal.setCurrencyID(CurrencyCodeType.USD);
      }
      details.setShippingTotal(shippingTotal);
    }
    if (_request.getParameter(PARAM_ADDRESS_CITY_NAME) != null &&
        _request.getParameter(PARAM_ADDRESS_COUNTRY) != null &&
        _request.getParameter(PARAM_ADDRESS_NAME) != null &&
        _request.getParameter(PARAM_ADDRESS_PHONE) != null &&
        _request.getParameter(PARAM_ADDRESS_POSTAL_CODE) != null &&
        _request.getParameter(PARAM_ADDRESS_STATE_OR_PROVINCE) != null &&
        _request.getParameter(PARAM_ADDRESS_STREET1) != null) {
      details.setShipToAddress(buildAddress(_request));
    }
    if (_request.getParameter(PARAM_TAX_TOTAL) != null) {
      BasicAmountType taxTotal = new BasicAmountType();
      taxTotal.set_value(_request.getParameter(PARAM_TAX_TOTAL));
      if (_request.getParameter(PARAM_TAX_TOTAL_CURRENCY_ID) != null) {
        taxTotal.setCurrencyID(CurrencyCodeType.fromString(_request
            .getParameter(PARAM_TAX_TOTAL_CURRENCY_ID)));
      }
      else {
        taxTotal.setCurrencyID(CurrencyCodeType.USD);
      }
      details.setTaxTotal(taxTotal);
    }
    return details;
  }

  public static final DoDirectPaymentRequestDetailsType buildDoDirectPaymentRequestDetails(HttpServletRequest _request) {
    DoDirectPaymentRequestDetailsType details = new DoDirectPaymentRequestDetailsType();
   
    CreditCardDetailsType cardDetails = new CreditCardDetailsType();
    PayerInfoType cardOwner = new PayerInfoType();
    cardOwner.setAddress(buildAddress(_request));
    if (_request.getParameter(PARAM_PAYER) != null) {
      cardOwner.setPayer(_request.getParameter(PARAM_PAYER));
    }
    PersonNameType name = new PersonNameType();
    if (_request.getParameter(PARAM_FIRST_NAME) != null) {
      name.setFirstName(_request.getParameter(PARAM_FIRST_NAME));
    }
    if (_request.getParameter(PARAM_LAST_NAME) != null) {
      name.setLastName(_request.getParameter(PARAM_LAST_NAME));
    }
    cardOwner.setPayerName(name);
    cardDetails.setCardOwner(cardOwner);
    if (_request.getParameter(PARAM_CREDIT_CARD_NUMBER) != null) {
      cardDetails.setCreditCardNumber(_request.getParameter(PARAM_CREDIT_CARD_NUMBER));
    }
    if (_request.getParameter(PARAM_CREDIT_CARD_TYPE) != null) {
      cardDetails.setCreditCardType(CreditCardTypeType.fromString(_request.getParameter(PARAM_CREDIT_CARD_TYPE)));
    }
    if (_request.getParameter(PARAM_CVV2) != null) {
      cardDetails.setCVV2(_request.getParameter(PARAM_CVV2));
    }
    if (_request.getParameter(PARAM_EXP_MONTH) != null) {
      cardDetails.setExpMonth(Integer.parseInt(_request.getParameter(PARAM_EXP_MONTH)));
    }
    if (_request.getParameter(PARAM_EXP_YEAR) != null) {
      cardDetails.setExpYear(Integer.parseInt(_request.getParameter(PARAM_EXP_YEAR)));
    }
    details.setCreditCard(cardDetails);
    if (_request.getParameter(PARAM_IP_ADDRESS) != null) {
      details.setIPAddress(_request.getParameter(PARAM_IP_ADDRESS));
    }
    if (_request.getParameter(PARAM_MERCHANT_SESSION_ID) != null) {
      details.setMerchantSessionId(_request.getParameter(PARAM_MERCHANT_SESSION_ID));
    }
    if (_request.getParameter(PARAM_PAYMENT_ACTION) != null) {
      details.setPaymentAction(PaymentActionCodeType.fromString(_request.getParameter(PARAM_PAYMENT_ACTION)));
    }
    details.setPaymentDetails(buildPaymentDetails(_request));
    return details;
  }
 
  public static final void writeXML(Object _response, HttpServletResponse _httpResponse) throws MarshalException, ValidationException, IOException, MappingException {
    _httpResponse.addHeader("Content-Type", "application/xml");
    StringWriter writer = new StringWriter();
    Marshaller marshaller = new Marshaller(writer);
    marshaller.setMapping(MAPPING);
    marshaller.marshal(_response);
    if (LOG.isDebugEnabled()) {
      LOG.debug("Writing XML [" + writer.toString() + "]");
    }
    _httpResponse.getWriter().write(writer.toString());
  }

}
TOP

Related Classes of org.javacommerce.paypal.APIUtil

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.